//
//  ProdPromotionShowView.m
//  JiaoX
//
//  Created by yinqy on 15/2/2.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "ProdPromotionShowView.h"

#define PARENT_VIEW_MARGIN [DimensUtils getScaleX:34]
#define CHILD_VIEW_MARGIN [DimensUtils getScaleX:20]
#define PARENT_WIDTH JXScreenW-PARENT_VIEW_MARGIN*2
#define TITLE_FONT [DimensUtils getScaleX:15]

@implementation ProdPromotionShowView
- (instancetype)initWithImageUrl:(NSString *)urlStr title:(NSString *)title
{
    self = [super init];
    if (self) {
        [self setFrame:CGRectMake(0, 0, JXScreenW, JXScreenH)];
        
        
        //灰色背景
        UIView *bg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, JXScreenW, JXScreenH)];
        [bg setAlpha:0.6];
        [bg setBackgroundColor:[UIColor blackColor]];
        bg.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeSelf)];
        [bg addGestureRecognizer:singleTap];
        [self addSubview:bg];
        
        
        //父view
        UIView *view = [[UIView alloc]init];
        [view.layer setCornerRadius:5];
        [view.layer setMasksToBounds:YES];
        [view setBackgroundColor:[UIColor whiteColor]];
        
        //iamge
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(CHILD_VIEW_MARGIN, CHILD_VIEW_MARGIN, PARENT_WIDTH-CHILD_VIEW_MARGIN*2, PARENT_WIDTH-CHILD_VIEW_MARGIN*2)];
        [image sd_setImageWithURL:[NSURL URLWithString:urlStr]];
        [view addSubview:image];
        
        //label
        UILabel *label = [[UILabel alloc]init];
        
        [label setText:title];
        [label setFont:[UIFont systemFontOfSize:TITLE_FONT]];
        CGSize size = [title sizeWithFont:label.font constrainedToSize:CGSizeMake(image.width, MAXFLOAT)];
        [label setFrame:(CGRect){{CHILD_VIEW_MARGIN,CGRectGetMaxY(image.frame)+CHILD_VIEW_MARGIN},size}];
        [label setNumberOfLines:0];
        [view addSubview:label];
        
        
        
        CGFloat viewX = PARENT_VIEW_MARGIN;
        CGFloat viewH = CGRectGetMaxY(label.frame)+CHILD_VIEW_MARGIN;
        CGFloat viewY = (self.height-viewH)*0.5;
        CGFloat viewW = PARENT_WIDTH;
        
        [view setFrame:CGRectMake(viewX, viewY, viewW, viewH)];
        
        //关闭按钮
        UIButton *btn = [[UIButton alloc]init];
        UIImage *closeImage = [UIImage imageNamed:@"ic_prod_close"];
        [btn setBackgroundImage:closeImage forState:UIControlStateNormal];
        [btn setSize:closeImage.size];
        [btn setCenter:CGPointMake(CGRectGetMaxX(view.frame), CGRectGetMinY(view.frame))];
        [btn addTarget:self action:@selector(removeSelf) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:view];
//        [self addSubview:btn];
    }
    return self;
}

-(void)removeSelf{
    [self removeFromSuperview];
}

@end
